/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-04-10
 * Time: 23:21
 */
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Solution {
    // 前缀和
    private int prefix;
    public TreeNode convertBST(TreeNode root) {
        if(root == null){
            return null;
        }
        dfs(root);
        return root;
    }

    public void dfs(TreeNode root){
        if(root == null){
            return;
        }
        dfs(root.right);
        root.val += prefix;
        prefix = root.val;
        dfs(root.left);
    }
}
